package com.diaohw.module.log.service.impl;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.diaohw.module.log.mapper.DbLogMapper;
import com.diaohw.module.log.mapper.RequestLogMapper;
import com.diaohw.module.log.obj.entity.DbLog;
import com.diaohw.module.log.obj.entity.RequestLog;
import com.diaohw.module.log.obj.query.LogQuery;
import com.diaohw.module.log.service.LogService;
import com.diaohw.platform.common.obj.page.PageResultVo;
import com.diaohw.platform.framework.db.utils.WrapperUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LogImpl implements LogService {

    @Resource
    RequestLogMapper requestLogMapper;
    @Resource
    DbLogMapper dbLogMapper;

    @Override
    public PageResultVo<RequestLog> httpList(LogQuery query) {
        LambdaQueryWrapper<RequestLog> wrapper = createWrapper(query);
        wrapper.select(RequestLog::getId, RequestLog::getLoginId, RequestLog::getLoginNickname, RequestLog::getLoginUsername,
                RequestLog::getResponseTime,RequestLog::getElapsed,RequestLog::getMethod,RequestLog::getMsg,RequestLog::getUri,
                RequestLog::getApplicationIp,RequestLog::getApplicationName,
                RequestLog::getRequestId,RequestLog::getLinkId,RequestLog::getStatus,
                RequestLog::getRequestIp, RequestLog::getRequestTime, RequestLog::getDescription);
        wrapper.orderByAsc(RequestLog::getLinkId)
                .orderByDesc(RequestLog::getCreatedTime);
        Page<RequestLog> page = requestLogMapper.selectPage(WrapperUtils.createPage(query), wrapper);
        return PageResultVo.create(page.getTotal(), page.getRecords());
    }

    @Override
    public PageResultVo<DbLog> dbList(LogQuery query) {
        LambdaQueryWrapper<DbLog> wrapper = Wrappers.lambdaQuery(DbLog.class)
                .eq(StrUtil.isNotBlank(query.getSqlType()), DbLog::getSqlType, query.getSqlType())
                .eq(StrUtil.isNotBlank(query.getRequestId()), DbLog::getRequestId, query.getRequestId())
                .eq(ObjUtil.isNotNull(query.getStatus()), DbLog::getStatus, query.getStatus())
                .ge(ObjUtil.isNotNull(query.getElapsed()), DbLog::getElapsed, query.getElapsed())
                .eq(StrUtil.isNotBlank(query.getLoginId()), DbLog::getLoginId, query.getLoginId())
                .eq(StrUtil.isNotBlank(query.getApplicationNam()), DbLog::getApplicationName, query.getApplicationNam())
                .eq(StrUtil.isNotBlank(query.getApplicationIp()), DbLog::getApplicationIp, query.getApplicationIp());

        WrapperUtils.time(wrapper, DbLog::getCreatedTime, query.getStartTime(), query.getEndTime());
        wrapper.orderByAsc(DbLog::getLinkId)
                .orderByDesc(DbLog::getCreatedTime);
        Page<DbLog> page = dbLogMapper.selectPage(WrapperUtils.createPage(query), wrapper);
        return PageResultVo.create(page.getTotal(), page.getRecords());
    }

    @Override
    public Boolean deleteRequestLog(LogQuery query) {
        LambdaQueryWrapper<RequestLog> wrapper = createWrapper(query);
        int delete = requestLogMapper.delete(wrapper);
        return delete > 0;
    }

    @Override
    public Boolean deleteRequestLog(List<String> logIds) {
        return requestLogMapper.deleteBatchIds(logIds) > 0;
    }

    @Override
    public LambdaQueryWrapper<RequestLog> createWrapper(LogQuery query) {
        LambdaQueryWrapper<RequestLog> wrapper = Wrappers.lambdaQuery(RequestLog.class);
        wrapper.eq(StrUtil.isNotBlank(query.getRequestId()), RequestLog::getRequestId, query.getRequestId())
                .eq(ObjUtil.isNotNull(query.getStatus()), RequestLog::getStatus, query.getStatus())
                .ge(ObjUtil.isNotNull(query.getElapsed()), RequestLog::getElapsed, query.getElapsed())
                .eq(StrUtil.isNotBlank(query.getLoginId()), RequestLog::getLoginId, query.getLoginId())
                .eq(StrUtil.isNotBlank(query.getApplicationNam()), RequestLog::getApplicationName, query.getApplicationNam())
                .eq(StrUtil.isNotBlank(query.getApplicationIp()), RequestLog::getApplicationIp, query.getApplicationIp())
                .eq(StrUtil.isNotBlank(query.getRequestIp()), RequestLog::getRequestIp, query.getRequestIp())
                .eq(StrUtil.isNotBlank(query.getMethod()), RequestLog::getMethod, query.getMethod());

        WrapperUtils.time(wrapper, RequestLog::getCreatedTime, query.getStartTime(), query.getEndTime());
        return wrapper;
    }

}
